#include "../Tools/GLHeaders.hpp"
#include "ModelTrunk.hpp"
#include <cmath>

ModelTrunk::ModelTrunk(void){
	activate = false;	
  list_id = glGenLists(1); // create one DisplayList
  glNewList(list_id, GL_COMPILE);
  GLUquadricObj *quadratic = gluNewQuadric();
  gluQuadricNormals(quadratic, GLU_SMOOTH);

  GLfloat pt00[] = {-1.5f, 1.0f, 1.0f};
  GLfloat pt01[] = { 1.5f, 1.0f, 1.0f};
  GLfloat pt02[] = { 1.5f, 3.0f, 1.0f};
  GLfloat pt03[] = { 3.0f, 8.0f, 1.0f};
  GLfloat pt04[] = { 3.0f, 9.5f, 1.0f};
  GLfloat pt05[] = { 0.0f,10.0f, 1.0f};
  GLfloat pt06[] = {-3.0f, 9.5f, 1.0f};
  GLfloat pt07[] = {-3.0f, 8.0f, 1.0f};
  GLfloat pt08[] = {-1.5f, 3.0f, 1.0f};
  GLfloat pt09[] = { 0.0f, 8.0f, 1.0f};

  GLfloat pt10[] = {-1.5f, 1.0f,-1.0f};
  GLfloat pt11[] = { 1.5f, 1.0f,-1.0f};
  GLfloat pt12[] = { 1.5f, 3.0f,-1.0f};
  GLfloat pt13[] = { 3.0f, 8.0f,-1.0f};
  GLfloat pt14[] = { 3.0f, 9.5f,-1.0f};
  GLfloat pt15[] = { 0.0f,10.0f,-1.0f};
  GLfloat pt16[] = {-3.0f, 9.5f,-1.0f};
  GLfloat pt17[] = {-3.0f, 8.0f,-1.0f};
  GLfloat pt18[] = {-1.5f, 3.0f,-1.0f};
  GLfloat pt19[] = { 0.0f, 8.0f,-1.0f};

  GLfloat pt20[] = {-3.75f, 8.5f, 0.25f};
  GLfloat pt21[] = {-3.00f, 8.5f, 0.25f};
  GLfloat pt22[] = {-3.00f, 9.0f, 0.25f};
  GLfloat pt23[] = {-3.75f, 9.0f, 0.25f};

  GLfloat pt30[] = {-3.75f, 8.5f,-0.25f};
  GLfloat pt31[] = {-3.00f, 8.5f,-0.25f};
  GLfloat pt32[] = {-3.00f, 9.0f,-0.25f};
  GLfloat pt33[] = {-3.75f, 9.0f,-0.25f};

  GLfloat pt40[] = { 3.00f, 8.5f, 0.25f};
  GLfloat pt41[] = { 3.75f, 8.5f, 0.25f};
  GLfloat pt42[] = { 3.75f, 9.0f, 0.25f};
  GLfloat pt43[] = { 3.00f, 9.0f, 0.25f};

  GLfloat pt50[] = { 3.00f, 8.5f,-0.25f};
  GLfloat pt51[] = { 3.75f, 8.5f,-0.25f};
  GLfloat pt52[] = { 3.75f, 9.0f,-0.25f};
  GLfloat pt53[] = { 3.00f, 9.0f,-0.25f};
  //Trunk
  glBegin(GL_TRIANGLES);
  glNormal3f( 0.0f, 0.0f, 1.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt00); // Front
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt01);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt02);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt02);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt08);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt00);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt02);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt03);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt09);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt08);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt02);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt09);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt08);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt09);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt07);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt07);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt03);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt05);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt05);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt03);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt04);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt07);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt05);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt06);

  glNormal3f( 1.0f, 0.0f, 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt01); // Right
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt11);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt12);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt12);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt02);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt01);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt03);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt13);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt14);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt14);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt04);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt03);

  glNormal3f(-1.0f, 0.0f, 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt10); // Left
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt00);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt08);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt08);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt18);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt10);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt17);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt07);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt06);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt06);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt16);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt17);

  glNormal3f( 0.0f, 0.0f,-1.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt11); // Back
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt10);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt18);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt18);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt12);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt11);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt18);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt17);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt19);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt18);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt19);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt12);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt12);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt19);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt13);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt13);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt17);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt15);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt17);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt16);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt15);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt13);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt15);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt14);

  glNormal3f( 0.0f,-1.0f, 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt01); // Bottom
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt00);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt10);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt10);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt11);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt01);

  glNormal3f( cosf(3.0f*M_PI/8.0f), sinf(3.0f*M_PI/8.0f), 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt05); // Right Top
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt04);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt14);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt14);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt15);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt05);

  glNormal3f( cosf(5.0f*M_PI/8.0f), sinf(5.0f*M_PI/8.0f), 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt06); // Left Top
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt05);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt15);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt15);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt16);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt06); 

  glNormal3f( cosf(-M_PI/8.0f), sinf(-M_PI/8.0f), 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt02); // Right Bottom
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt12);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt13);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt13);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt03);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt02); 

  glNormal3f( cosf(-7.0f*M_PI/8.0f), sinf(-7.0f*M_PI/8.0f), 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt18); // Left Bottom
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt08);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt07);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt07);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt17);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt18);

  //Left Hip
  glNormal3f( 0.0f, 0.0f, 1.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt20); // Front
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt21);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt22);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt22);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt23);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt20);

  glNormal3f( 0.0f, 1.0f, 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt23); // Top
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt22);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt32);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt32);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt33);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt23);

  glNormal3f( 0.0f, 0.0f,-1.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt31); // Back
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt30);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt33);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt33);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt32);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt31);

  glNormal3f( 0.0f,-1.0f, 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt21); // Bottom
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt20);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt30);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt30);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt31);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt21);

  //Right Hip
  glNormal3f( 0.0f, 0.0f, 1.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt40); // Front
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt41);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt42);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt42);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt43);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt40);

  glNormal3f( 0.0f, 1.0f, 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt43); // Top
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt42);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt52);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt52);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt53);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt43);

  glNormal3f( 0.0f, 0.0f,-1.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt51); // Back
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt50);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt53);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt53);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt52);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt51);

  glNormal3f( 0.0f,-1.0f, 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt41); // Bottom
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt40);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt50);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt50);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt51);
  glColor3f(0.4f, 0.4f, 0.4f);glVertex3fv(pt41);
  glEnd();

  //Bottom Sphere
  glColor3f(0.4f, 0.4f, 0.4f);
  gluSphere(quadratic, 1.5f, 16, 16);
 
  //Left Sphere
  glTranslatef(-4.0f, 8.75f, 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);
  gluSphere(quadratic, 1.0f, 16, 16);
  glTranslatef( 4.0f,-8.75f, 0.0f);
  
  //Right Sphere
  glTranslatef( 4.0f, 8.75f, 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);
  gluSphere(quadratic, 1.0f, 16, 16);
  glTranslatef(-4.0f,-8.75f, 0.0f);

  //Head Sphere
  glTranslatef( 0.0f, 10.0f, 0.0f);
  glColor3f(0.4f, 0.4f, 0.4f);
  gluSphere(quadratic, 0.5f, 16, 16);
  glTranslatef( 0.0f,-10.0f, 0.0f);
  glEndList();
 }

ModelTrunk::~ModelTrunk(void){
  glDeleteLists(list_id,1);
}

void ModelTrunk::Draw(void){
	glPushAttrib(GL_COLOR_BUFFER_BIT|GL_CURRENT_BIT|GL_TEXTURE_BIT);	
	glDisable(GL_TEXTURE_2D); 
	if(activate)
		glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
	else
		glColor4f(0.0f, 1.0f, 0.0f, 1.0f);

  glCallList(list_id);
	glPopAttrib();
}

void ModelTrunk::Activate(void){
	activate = true;
}

void ModelTrunk::Desactivate(void){
	activate = false;
}
